
function import_fixtfeqs(cl,icl,ivers) {
  // Try to resolv tf eqs through imported field
  var tf_iter= new claim_features_iterator(cl);
  var tfeq_iter,tfeq,tfimpeq,tffound;  
  while(!tf_iter.end()) {              
    tfeq=tf_iter.getEq();
    tf_iter.setEq(""); // clear tf eq, if resolvable, fill in later
    tf_iter.setImpEq(ivers+"/"+tfeq); // store original eq.
    // try to find imported tfeq
    tffound=0;      
    if(tfeq) {
      //alert(tfeq);
      tfimpver=ivers+"/"+tfeq; // import field should look like this 
      for(var n=1; n< cl && !tffound; n++) {
	// find features in claim n with imported field equal to tfimpver
	tfeq_iter=new claim_features_iterator(n);
	while(!tfeq_iter.end()) {
	  if(tfeq_iter.getImp()==tfimpver) {	    
	    tf_iter.setEq(n+"."+tfeq_iter.gettf()); // resolve equivalence
	    tffound=1;
	    break;
	  }
	  tfeq_iter.next();
	}
      }
    }      
    tf_iter.next();
  }   
}


// Handle empty import
// replace a claim with a previous version of the claim 


function claim_import_group(cl,icl,ivers) {
  var fromgroup=GetClaimNodeFromVers(icl,ivers).getAttribute("G");
  if(fromgroup) {
    if((XML_CLM._usesame && XML_CLM._usegroup) || (!XML_CLM._usesame  && confirm("Imported claim belongs to group "+
										 fromgroup+", keep group ?"))) {
      XML_CLM._usegroup=1;
      if(GetGroupsLength()<GetGroupIdx(fromgroup)) {
	if(confirm("Group "+fromgroup+" does not exist, copy groups from set #"+ivers+" ?")) {
	  CloneGroups(ivers);
	} else {
	  alert("Aborting import");
	  return false;
	}  
      }
      SetClaimGroup(cl,fromgroup);
    } else {
      SetClaimGroup(cl,"");
    }
    
    if(!XML_CLM._usesame && XML_CLM._usegroup && !XML_CLM._alreadyasked && confirm("Automatically import groups ?")) {
      XML_CLM._usesame=1;
      XML_CLM._alreadyasked=1;
    }
  }
}

function import_fixeqs(cl,icl,ivers,ieq) {
  if(ieq) { // equivalence in previous set
    var ref;
    var eqfound=0;
    // try to find claim importing equivalent claim
    for(var n=1; n< cl; n++) {
      ref= new claim_vtf(GetClaimImport(n));
      if((!ref.getver() || ref.getver()==ivers) && ref.getcl()==ieq) {
	eqfound=n;
	alert("Claim equivalence resolved to "+n+" because EQ("+ivers+"/"+
	      icl+")=="+ivers+"/"+ieq+" and IMP("+n+")=="+ivers+"/"+ieq);
	SetClaimEq(cl,n);
	break;
      }
    }
    if(!eqfound) {  // try to find claim equivalent to same missing claim
      var missing=ivers+"/"+ieq;
      for(var n=1; n< cl; n++) {
	if(GetClaimImpEq(n)== missing) {
	  alert("Claim equivalence resolved to "+n+" because EQ("+ivers+"/"+
		icl+")==EQ("+ivers+"/"+n+")");
	  eqfound=n;
	  SetClaimEq(cl,n); 
	  break;
	}
      }      
      if(!eqfound) { // keep missing equivalent claim to reconstruct other equivalences
	SetClaimImpEq(cl,ivers+"/"+ieq);
	alert("Unsolved equivalence import EQ("+ivers+"/"+icl+")=="+
	      ivers+"/"+ieq+" (equivalent claim not imported or missing)");
	SetClaimEq(cl,"");  		
      }
    }
  }
  return GetClaimEq(cl);
}

function importfiximpeqs(impeqs) {
  for(var ieq in impeqs) {
    var usingit=new query_features_iterator("@impeq=\""+ieq+"\"");
    while(!usingit.end()) {
      var cltf=new claim_vtf(usingit.gettxt());
      import_fixeqs(cl,icl,iver,ieq);	
      usingit.next();
    }
  }
}


function claim_replace(cl,icl,iver) {  
  if(!icl) { // delete import
    if(GetClaimImport(cl)) { // delete previous import
      ReplaceClaim(cl,GetClaimSetVersion());  // replace claim with empty one
      SetClaimImport(cl,"");
    }
    return;
  }
  if(iver >= 1) {
    var ref= new claim_vtf(iver,icl);
    if(ref.isOK()) {
      if(ref.getver()) {
	ver=ref.getver();
	icl=ref.getcl();
      }      
      if(ReplaceClaim(cl,GetClaimSetVersion(),icl,iver)) {
	SetClaimImport(cl,iver+"/"+icl);
      }
    }
  }
}

function claim_merge(cl,icl,iver,lang) {
  var ref= new claim_vtf(iver,icl);
  if(ref.isOK()) {
    if(ref.getver()) {
      ver=ref.getver();
      icl=ref.getcl();
    }    
    var newtf=1;
    var tf_iter=new claim_features_iterator(cl);
    while(newtf <= GetFeaturesLength(cl)) {
      var tf=newtf++;
      if(!GetFeatureImport(cl,tf) || GetFeaturesLength(cl)==1) {
	var itf_iter= new claim_features_iterator(icl,iver);
	//alert("Import none splitted "+iver+"/"+icl+".1"); // import not modified 
	var newdesc  =partrim(clean_text(GetFeatureDesc(cl,tf),lang,"basic"));
	while(!itf_iter.end()) {
	  var olddesc  =partrim(clean_text(itf_iter.getDesc(),lang,"basic")).toLowerCase();
	  var lownewdesc=newdesc.toLowerCase();
	  //alert("find ["+olddesc+"] in ["+lownewdesc+"]");
	  var addextra="";
	  if((idx=lownewdesc.indexOf(olddesc))>=0){   
	    alert("Merged in: "+itf_iter.gettxt());
	    if(idx>0) { // part not found
	      extra=trim(newdesc.substring(0,idx));
	      if(extra) {
		if(is_claim_separator((extra))){
		  addextra=newdesc.substring(0,idx); // not worth a feature
		} else { 
		  //alert("Extra portie "+extra);
		  SetFeatureDesc(cl,tf,newdesc.substring(0,idx)); // new portion
		  InsertFeatureAfter(cl,tf++,"");
		}
	      }
	    }
	    SetFeatureDesc(cl,tf,addextra+newdesc.substring(idx,idx+olddesc.length));  
	    ImportPassages(cl,tf,iver,icl,itf_iter.gettf());
	    SetFeatureImport(cl,tf,iver+"/"+icl+"."+itf_iter.gettf());
	    newdesc=newdesc.substring(idx+olddesc.length);
	    if(newdesc) {
	      //alert("Remain portie "+newdesc);
	      InsertFeatureAfter(cl,tf++,newdesc);
	    }
	  } 
	  itf_iter.next();	
	}	
      }
    }
  }
}


function claim_import(cl,icl,iver,lang) {  
  //alert("claim import from "+iver+"/"+icl);

  var origimpeqs=[];
  var tf_iter= new claim_features_iterator(cl);
  while(!tf_iter.end()){
    if(tf_iter.getImp()) {
      origimpeqs.push(tf_iter.getImp());
    }
    tf_iter.next();
  }
  
  if(!icl) { // delete import
    if(GetClaimImport(cl)) { // delete previous import
      ReplaceClaim(cl,GetClaimSetVersion());  // replace claim with empty one
      SetClaimImport(cl,"");
    }
    return;
  }
  if(iver >= 1) {    
    // check if whole claim is equivalent to other claim.
    var ieq=GetClaimAttrFromVers(icl,iver,"eq");
    if(ieq) {
      if(import_fixeqs(cl,icl,iver,ieq)){
	//alert("solved by setting eq to "+GetClaimEq(cl)); 
	claim_import_group(cl,ieq,iver);  
	return;
      } 
      alert("Redirect import to "+iver+"/"+ieq); 
      icl=ieq;
    }
    var idx;	
    var ref= new claim_vtf(iver,icl);
    if(ref.isOK()) {
      if(ref.getver()) {
	var _iver=ref.getver();
	var _icl=ref.getcl();
      }    
      claim_import_group(cl,_icl,_iver);

      var tf=1;
      var itf_iter= new claim_features_iterator(_icl,_iver);
      if(GetFeaturesLength(cl)==1) {
	//alert("Import none splitted "+iver+"/"+icl+".1"); // import not modified 
	var newdesc  =partrim(clean_text(GetFeatureDesc(cl,1),lang,"basic"));
	while(!itf_iter.end()) {
	  var olddesc  =partrim(clean_text(itf_iter.getDesc(),lang,"basic")).toLowerCase();
	  var lownewdesc=newdesc.toLowerCase();
	  //alert(txt2+" vs "+cleandesc);
	  var addextra="";
	  if((idx=lownewdesc.indexOf(olddesc))>=0){
	    //alert("One Found at "+idx);
	    if(idx>0) { // part not found
	      extra=trim(newdesc.substring(0,idx));
	      if(extra) {
		if(is_claim_separator((extra))){
		  addextra=newdesc.substring(0,idx); // not worth a feature
		} else { 
		  //alert("Extra portie "+extra);
		  SetFeatureDesc(cl,tf,newdesc.substring(0,idx)); // new portion
		  InsertFeatureAfter(cl,tf++,"");
		}
	      }
	    }
	    SetFeatureDesc(cl,tf,addextra+newdesc.substring(idx,idx+olddesc.length));  
	    ImportPassages(cl,tf,_iver,_icl,itf_iter.gettf());
	    SetFeatureImport(cl,tf,_iver+"/"+_icl+"."+itf_iter.gettf());
	    newdesc=newdesc.substring(idx+olddesc.length);
	    if(newdesc) {
	      //alert("Remain portie "+newdesc);
	      InsertFeatureAfter(cl,tf++,newdesc);
	    }
	  } else if(itf_iter.length()==1) { // if nothing found and one  force importing 
	    //alert("Import tf 1 from "+iver+"/"+icl+".1"); // unconditional import 
	    ImportPassages(cl,tf,_iver,_icl,tf);
	    SetFeatureImport(cl,tf,_iver+"/"+_icl+"."+tf);
	  }
	  itf_iter.next();	  
	}
      } else {
	//alert("Claim "+cl+" is splitted, cannot import");
	ImportPassages(cl,1,_iver,_icl,1);
	SetFeatureImport(cl,1,_iver+"/"+_icl+"."+1);	
      }
      SetClaimImport(cl,_iver+"/"+_icl);
      import_fixtfeqs(cl,_icl,_iver);            
    } else { // ref is empty
      alert("Ref is empty");            
    }
  }
  UpdateClaimAutoValAll(cl);
}

